package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.domain.Travelcontent;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 推荐游记资源管理控制器
 */
@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {
    @Autowired
    ITravelCommendService travelCommendService;

    /**
     * 推荐游记资源分页
     * @return
     */
    @RequestMapping("/list")
    public String auditquery(@ModelAttribute("qo") TravelCommendQueryObject qo, Model model){
        //查询全部推荐
        qo.setOrderBy("schedule desc");//按照推荐时间倒叙排列
        model.addAttribute("pageInfo",travelCommendService.query(qo));
        return "travelCommend/list";
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(TravelCommend travelCommend, MultipartFile file){
        //上传前台传来的图片
        if (file!=null&&file.getSize()>0){
            String upload = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            //将新的封面设置给推荐
            travelCommend.setCoverUrl(upload);
        }
        travelCommendService.saveOrUpdate(travelCommend);
        return new JSONResult();
    }
}
